home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0058_ASM Bit Functions.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  3KB  |  87 lines

  1. (***********************************************************************
  2.  BitFuncs: Bit manipulation TPU, (C) September 1994 by William Barath
  3.        Free for use in commercial and non-commercial software.
  4.           Compile with $s-,g+ and optional word alignment
  5. (**********************************************************************)
  6. Unit BitFuncs; Interface
  7. Const Bits:array[0..7] of Byte = ($1,$2,$4,$8,$10,$20,$40,$80);
  8. Procedure SetBitA(var a;Bit:Word);
  9. Procedure ClearBitA(var a;Bit:Word);
  10. Procedure InvertBitA(var a;Bit:Word);
  11. Function  GetBitA(var a;Bit:Word):Boolean;
  12. Procedure SetBit(var a;Bit:Word);
  13. Procedure ClearBit(var a;Bit:Word);
  14. Procedure InvertBit(var a;Bit:Word);
  15. Function  GetBit(a:Byte;Bit:Word):Boolean;
  16. Implementation
  17. Procedure SetBitA(var a;Bit:Word);assembler;
  18. asm
  19.   Les di,a              {get variable reference}
  20.   Mov si,bit            {get bit offset}
  21.   Mov bx,si             {index into memory with bit Div 8}
  22.   And si,07h            {and index into 'bits' with bit Mod 8}
  23.   Shr bx,03h
  24.         Mov al,Byte(Bits[si]) {get the bit mask}
  25.   Or  es:[di+bx],al     {read, modify, and write back the data}
  26. end;
  27. Procedure ClearBitA(var a;Bit:Word);assembler;
  28. asm
  29.   Les di,a
  30.   Mov si,bit
  31.   Mov bx,si
  32.   And si,07h
  33.   Shr bx,03h
  34.   Mov al,Byte(Bits[si])
  35.   Not al                {invert the bit mask}
  36.   And es:[di+bx],al     {mask off the selected bit}
  37. end;
  38. Procedure InvertBitA(var a;Bit:Word);assembler;
  39. asm
  40.   Les di,a
  41.   Mov si,bit
  42.   Mov bx,si
  43.   And si,07h
  44.   Shr bx,03h
  45.   Mov al,Byte(Bits[si])
  46.   Xor es:[di+bx],al     {invert the selected bit}
  47. end;
  48. Function GetBitA(var a;Bit:Word):Boolean;assembler;
  49. asm
  50.   Les di,a
  51.   Mov si,bit
  52.   Mov bx,si
  53.   And si,07h
  54.   Shr bx,03h
  55.   Mov al,Byte(Bits[si])
  56.   And al,es:[di+bx]     {return the selected bit without writing}
  57. end;
  58. Procedure SetBit(var a;Bit:Word);assembler;
  59. asm
  60.   Les di,a              {get variable reference}
  61.   Mov si,bit            {get bit offset}
  62.   Mov al,Byte(Bits[si]) {get the bit mask}
  63.   Or  es:[di],al        {read, modify, write...}
  64. end;
  65. Procedure ClearBit(var a;Bit:Word);assembler;
  66. asm
  67.   Les di,a
  68.   Mov si,bit
  69.   Mov al,Byte(Bits[si])
  70.   Not al
  71.   And es:[di],al
  72. end;
  73. Procedure InvertBit(var a;Bit:Word);assembler;
  74. asm
  75.   Les di,a
  76.   Mov si,bit
  77.   Mov al,Byte(Bits[si])
  78.   Xor es:[di],al
  79. end;
  80. Function GetBit(a:byte;Bit:Word):Boolean;assembler;
  81. asm
  82.   Mov al,a              {expect the data on the stack}
  83.   Mov si,bit            {get the bit index}
  84.   And al,Byte(Bits[si]) {mask data with bitmask and return it}
  85. end;
  86. end.
  87.